<template>
  <div class="page-container">
    <!-- <div class="q-search">
      <div class="q-item" v-for="(item, index) in quickSearchs" :key="index">
        <div class="q-title">{{ item.name }}</div>
        <div>
          <span class="q-price">{{ item.num }}</span>
          <span> 元</span>
        </div>
      </div>
    </div> -->
    <list-searcher :searchList="searchList" v-model:searchForm="queryParam" @loadData="onSearch" />
    <table-index :table="table" @loadData="loadData">
      <template #table-summary>
        <div class="table-info">
          <span class="table-info-item">
            <a-tooltip content="当前账单，总实收金额-总实付金额，即账单实结金额之和">
              <icon-exclamation-circle style="font-size: 16px; color: #165dff" />
            </a-tooltip>
            总收入：<span class="num">{{ unitVal(objectFieldInfo?.allPay, '元') }}</span>
          </span>
          <span class="table-info-item">
            账单总数：<span class="num">{{ table.pagination.pageCount || 0 }}</span>
          </span>
          <span class="table-info-item">
            应收金额：<span class="num">{{ unitVal(objectFieldInfo?.receiveAmount, '元') }}</span>
          </span>
          <span class="table-info-item">
            实收金额：<span class="num">{{ unitVal(objectFieldInfo?.payAmount, '元') }}</span>
          </span>
          <span class="table-info-item">
            应付金额：<span class="num">{{ unitVal(objectFieldInfo?.receiveFeeAmount, '元') }}</span>
          </span>
          <span class="table-info-item">
            实付金额：<span class="num">{{ unitVal(objectFieldInfo?.feeAmount, '元') }}</span>
          </span>
        </div>
      </template>
      <template #orderInfo="{ record }">
        <div class="order-info-item">
          自有
          <div>{{ record.orderCode }}</div>
        </div>
        <div class="order-info-item" v-if="record.channelOrderCode">
          渠道
          <div>{{ record.channelOrderCode }}</div>
        </div>
        <div class="order-info-item">{{ record.orderCreateTime }}</div>
        <!-- 订单标签 -->
        <div class="order-info-item" v-if="record.orderLabelList && record.orderLabelList.length > 0">
          <tag type="primary" v-for="(item, index) in record.orderLabelList" :key="index">{{ item }}</tag>
        </div>
      </template>
      <template #carInfo="{ record }">
        <div class="link-wrapper">
          <!-- 车牌号显示 -->
          <div v-if="record.carLicenseNo">{{ record.carLicenseNo }}</div>
          <!-- 车型名称 -->
          <div v-if="record.carModelName">{{ record.carModelName }}</div>
        </div>
      </template>
      <!-- 承租人信息 -->
      <template #rentMember="{ record }">
        <div class="link-wrapper">
          <div v-if="record.memberName">{{ record.memberName }}</div>
          <div v-if="record.memberPhone">{{ record.memberPhone }}</div>
        </div>
      </template>
      <!-- 租赁周期 -->
      <template #leaseRange="{ record }">
        <div class="content-item pickup">
          <div class="flex items-start content-item-name">
            <tag type="warn">取</tag>
            <div class="flex-1">{{ record.startTime }}</div>
          </div>
        </div>
        <div class="content-item return">
          <div class="flex items-start content-item-name">
            <tag type="success">还</tag>
            <div class="flex-1">{{ record.endTime }}</div>
          </div>
        </div>
      </template>
      <template #deposit="{ record }">
        <div class="content-item pickup">
          <div class="flex items-start content-item-name">
            <tag :type="['未收', '未退'].includes(record.carDepositStatusName) ? 'warn' : 'success'">{{ record.carDepositStatusName }}</tag>
            <div class="flex-1">{{ `租车押金：${record.carDeposit ? record.carDeposit : '0'}` }}</div>
          </div>
        </div>
        <div class="content-item" v-for="(item, index) in record.otherDepositList" :key="index">
          <div class="flex items-start content-item-name">
            <tag :type="['未收', '未退'].includes(item.depositStatusName) ? 'warn' : 'success'">{{ item.depositStatusName }}</tag>
            <div class="flex-1">
              {{ `${item.depositTypeName}：${item.receivedDepositTotalAmount ? item.receivedDepositTotalAmount : '0'}` }}
            </div>
          </div>
        </div>
        <div class="content-item return">
          <div class="flex items-start content-item-name">
            <tag :type="['未收', '未退'].includes(record.illegalDepositStatusName) ? 'warn' : 'success'">
              {{ record.illegalDepositStatusName }}
            </tag>
            <div class="flex-1">{{ `违章押金：${record.illegalDeposit ? record.illegalDeposit : '0'}` }}</div>
          </div>
        </div>
      </template>
    </table-index>
  </div>
</template>

<script setup lang="ts" name="BillShortOrderList">
  import { useTable, useFetchTableDataApi } from '@/hooks/table'
  import { getBillList, getBillStatistics } from '@/api/finance/bill'
  import { onMounted, reactive, computed, ref } from 'vue'
  import { usePageDict } from '@/hooks/useDict'
  import useNavigate from '@/hooks/useNavigate'
  import { unitVal } from '@/utils'

  const { navigateTo } = useNavigate()
  const dict = usePageDict('shortOrderDict')

  const quickSearchs = ref([
    { value: 50, name: '本月携程收入', num: 0 },
    { value: 70, name: '本月飞猪收入', num: 0 },
    { value: 90, name: '本月哈啰收入', num: 0 },
    { value: 110, name: '本月悟空收入', num: 0 },
    { value: 10, name: '本月代客下单', num: 0 },
    { value: 30, name: '本月自营小程序', num: 0 },
  ])

  // dateTime Attrs
  const dateTimeAttrs = {
    type: 'i-range-picker',
    attrs: {
      showTime: true,
      timePickerProps: {
        defaultValue: ['00:00:00', '23:59:59'],
      },
    },
  }

  const objectFieldInfo = ref()

  // 接口参数（不包含page信息）
  const queryParam = reactive<any>({})

  // 导出事件
  // const handleExport = () => {
  //   useExport(() => {}, queryParam)
  // }
  // 查询条件
  const searchList = computed(() => {
    return [
      { label: '订单号', prop: 'orderCodeOrChannelCode', placeholder: '请输入渠道/自有订单号', type: 'a-input' },
      { label: '承租人信息', prop: 'memberNameOrPhone', placeholder: '请输入承租人姓名/手机号', type: 'a-input' },
      { label: '订单标签', prop: 'orderLabels', type: 'a-select', options: dict.orderLabelEnum, multiple: true },
      {
        label: '下单时间',
        prop: 'orderCreateTimeRange',
        ...dateTimeAttrs,
        change: (value = []) => {
          queryParam.orderCreateTimeBegin = value[0]
          queryParam.orderCreateTimeEnd = value[1]
        },
      },
      {
        label: '预计取车时间',
        prop: 'startTimeRange',
        ...dateTimeAttrs,
        change: (value = []) => {
          queryParam.startTimeBeginTime = value[0]
          queryParam.startTimeEndTime = value[1]
        },
      },
      {
        label: '预计还车时间',
        prop: 'endTimeRange',
        ...dateTimeAttrs,
        change: (value = []) => {
          queryParam.endTimeBeginTime = value[0]
          queryParam.endTimeEndTime = value[1]
        },
      },
      { label: '订单来源', prop: 'orderSource', type: 'a-select', options: dict.orderSourceEnum },
      { label: '账单门店', prop: 'siteId', type: 'remote-select', attrs: { type: 'site_listBystaff' } },
      { label: '车牌号', prop: 'carLicenseNo', type: 'a-input' },
      { label: '车型', prop: 'carModelIds', type: 'remote-select', attrs: { type: 'car_model_tenantList_multiple' } },
      { label: '订单状态', prop: 'orderStatusList', type: 'a-select', options: dict.orderStatusEnum, multiple: true },
      {
        label: '实际取车时间',
        prop: 'actualStartTimeRange',
        ...dateTimeAttrs,
        change: (value = []) => {
          queryParam.actualStartTimeBeginTime = value[0]
          queryParam.actualStartTimeEndTime = value[1]
        },
      },
      {
        label: '实际还车时间',
        prop: 'actualEndTimeRange',
        ...dateTimeAttrs,
        change: (value = []) => {
          queryParam.actualEndTimeBeginTime = value[0]
          queryParam.actualEndTimeEndTime = value[1]
        },
      },
    ]
  })

  // 初始化
  const init = () => {
    loadData()
  }
  // table 配置
  const table = useTable({
    columns: [
      { title: '订单信息', dataIndex: 'orderInfo', width: 220, fixed: 'left', slotName: 'orderInfo' },
      { title: '车辆信息', dataIndex: 'carInfo', slotName: 'carInfo' },
      { title: '承租人', dataIndex: 'rentMember', slotName: 'rentMember' },
      { title: '订单来源', dataIndex: 'orderSourceName', width: 100 },
      { title: '租赁周期', dataIndex: 'leaseRange', slotName: 'leaseRange', width: 220 },
      { title: '账单门店', dataIndex: 'siteName' },
      { title: '费用金额', dataIndex: 'totalAmount', unit: '元', width: 120 },
      { title: '待结金额', dataIndex: 'receiveAmount', unit: '元', width: 120 },
      { title: '押金', dataIndex: 'deposit', unit: '元', slotName: 'deposit', width: 230 },
      { title: '订单状态', dataIndex: 'orderStatusName', width: 100 },
      { title: '订单备注', dataIndex: 'remark', ellipsis: true, width: 220 },
    ],
    operates: [
      {
        label: '详情',
        permission: 'svms:finance:bill:shortorder.view',
        method: (record: any) => {
          navigateTo(`/short-order/bill-info/detail/${record.id}`, `${record.orderCode}-账单详情`)
        },
      },
    ],
    // buttons: [
    //   {
    //     mode: 'import',
    //     type: 'primary',
    //     label: '导出',
    //     permission: 'svms:market:bazaar:member.export',
    //     method: () => handleExport(),
    //   },
    // ],
  })

  const getnum = () => {
    // getEnums().then((res) => {
    //   quickSearchs.value[0].num = res.result.notReceivedCount
    //   quickSearchs.value[1].num = res.result.receivedCount
    //   quickSearchs.value[2].num = res.result.wantDrawbackCount
    //   quickSearchs.value[3].num = res.result.finishDrawbackCount
    // })
  }

  // 列表接口调用
  const loadData = useFetchTableDataApi(table, queryParam, getBillList)

  const getBillStatisticsInfo = (queryParam?: any) => {
    getBillStatistics(queryParam).then((res) => {
      const { result } = res
      objectFieldInfo.value = result
    })
  }

  const onSearch = () => {
    queryParam.pageNumber = 1
    loadData(queryParam)
    getBillStatisticsInfo(queryParam)
  }

  onMounted(() => {
    init()
    getnum()
    getBillStatisticsInfo()
  })
</script>

<style lang="less" scoped>
  .table-info {
    display: flex;
    align-items: center;
    .table-info-item {
      display: flex;
      align-items: center;
      margin-right: 20px;
    }
  }
  .link-wrapper {
    font-size: 14px;
    .text-link {
      color: rgb(var(--primary-6));
      cursor: pointer;
    }
    .text-link + .text-link {
      margin-top: 6px;
    }
  }
  .order-info-item {
    .custom-tag {
      margin-right: 8px;
      margin-bottom: 10px;
    }
  }
  .order-info-item + .order-info-item {
    margin-top: 10px;
  }

  .content-item {
    .content-item-name {
      margin-bottom: 4px;
      .staff-can-click {
        color: rgb(var(--primary-6));
        cursor: pointer;
      }
      .tag-pickup {
        padding: 0 3px;
        line-height: 18px;
      }
      :deep(.custom-tag) {
        margin-right: 6px;
        position: relative;
        top: 1px;
      }
    }
    .content-item-tag :deep(.custom-tag.default) {
      line-height: 20px;
      padding: 4px 8px;
    }
  }
  .content-item + .content-item {
    margin-top: 10px;
  }
  .q-search {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-bottom: 20px;
    .q-item {
      width: 100%;
      height: 62px;
      display: flex;
      flex-direction: column;
      padding: 9px 16px;
      background-color: #ffffff;
      border-radius: 2px 2px 2px 2px;
      opacity: 1;
      border: 1px solid #e5e8ef;
      margin-right: 20px;
      cursor: pointer;

      .q-title {
        font-size: 10px;
        font-weight: 400;
        color: #4e5969;
      }

      .q-price {
        font-size: 20px;
        font-weight: 600;
        color: #1d2129;
      }
    }
  }
  .penal {
    height: 260px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
  }

  .num {
    font-size: 14px;
    font-weight: 400;
    color: #1890ff;
  }
</style>
